// 名字列表页
var api = require("../../utils/api.js")
Page({
  data: {
    curView: 'single',
    cue: 0,
    user: {},
    info: {},
    score: {},
    id: '',
    loading: true
  },
  // 处理数据
  dealArr(arr) {
    let temp = arr
    let tier = ['青铜', '白银', '黄金', '白金', '钻石', '大师', '皇家']
    let tierName = ['bronze', 'silver', 'gold', 'platinum', 'diamond', 'master', 'royalty']
    let type = ['single', 'double', 'five']
    if (arr.length > 0) {
      for (var i = 0; i < arr.length; i++) {
        temp[i].kills_per_match = arr[i].kills_per_match.toFixed(2)
        temp[i].wins_per_match = (arr[i].wins_per_match * 100).toFixed(2)
        temp[i].img = 'tier-' + tierName[temp[i].tier - 1] + '-' + temp[i].subtier
        temp[i].tier = tier[temp[i].tier - 1] + temp[i].subtier
        temp[i].type = type[temp[i].type - 1]
      }
      return temp
    } else {
      return
    }
  },
  dealType(arr) {
    let temp = arr
    let type = ['single', 'double', 'five']
    if (arr.length > 0) {
      for (var i = 0; i < arr.length; i++) {
        temp[i].type = type[temp[i].type - 1]
      }
      return temp
    } else {
      return
    }
  },
  // 切换tab
  changeView: function(event) {
    let self = this
    let index = 0
    let target = event.target;
    let dataset = event.target.dataset;
    switch (dataset.view) {
      case 'single':
        self.data.curView = 'single';
        index = 0;
        break;
      case 'double':
        self.data.curView = 'double';
        index = 1;
        break;
      case 'five':
        self.data.curView = 'five';
        index = 2;
        break;
    }

    this.setData({
      curView: this.data.curView,
      cur: index
    })
  },
  back() {
    wx.redirectTo({
      url: '../index/index'
    })
  },
  update() {
    let self = this;
    let id = this.data.id
    this.setData({
      loading: false,
    })
    api.http("h1z1/updateGameUserInfo", "POST", {
      userId: id
    }).then((res) => {
      if (res.data.code == 1) {
        self.getInfo(id);
        wx.showToast({
          title: '更新成功',
          icon: 'success',
          duration: 1000
        });
      }
    }).catch((res) => {
      self.setData({
        loading: true
      })
      wx.showToast({
        title: "数据获取失败",
        duration: 1000
      });
    })
  },
  // 相关信息
  getInfo(id) {
    let self = this;
    let bindStatus = false;
    this.setData({
      loading: false,
    })
    api.http("h1z1/getUserInfo", "POST", {
      userId: id
    }).then((res) => {
      if (res.data.code == 1) {
        let data = res.data.datas;
        if (data.h1z1) {
          let info = self.dealArr(data.h1z1.stats);
          let score = self.dealType(data.h1z1.matches);
          self.setData({
            user: data.wechat,
            info: info,
            score: score,
            loading: true
          })
        } else {
          self.setData({
            loading: true
          })
        }
      } else {
        self.setData({
          loading: true
        })
      }
    }).catch((res) => {
      self.setData({
        loading: true
      })
      wx.showToast({
        title: "数据获取失败",
        duration: 1000
      });
    })
  },
  // 分享
  onShareAppMessage: function() {
    return {
      title: 'h1z1群排行',
      path: 'pages/info/info?id=' + this.data.id
    }
  },
  onLoad: function(options) {
    let id = options.id
    this.data.id = id
    this.getInfo(id)
    wx.setNavigationBarTitle({
      title: '玩家详情'
    })
  }
})